<?php

namespace App\Http\Controllers\Admin\Configs;

use Illuminate\Http\Request;
use App\Http\Controllers\AdminController;

use App\Models\Admin\Permission;

use Auth;
use DB;

class PermissionController extends AdminController
{
    /**
     * 获取列表数据
     * @return [type] [description]
     */
    function getLists(Request $request){
        $pageIndex = (int)$request->input('page') ?  $request->input('page') : 1;
        $length    = (int)$request->input('length') ?  $request->input('length') : 10;
        $start     = ($pageIndex-1)*$length;

    	$where = [];
        $permissionModel = Permission::where($where);
        if($name=$request->input('name')){
            $permissionModel = $permissionModel->where('name', 'like', "%$name%");
        }

    	$data['total'] = $permissionModel->count();
    	if($data['total']>0){
    		$data['result'] = $permissionModel->orderBy('name','desc')->orderBy('updated_at','desc')->skip($start)->take($length)->get()->toArray();
    	}
    	$this->ajaxJson(0,'',$data);
    }

    /**
     * 获取所有权限
     * @return [type] [description]
     */
    function getAll(){
        $this->ajaxJson(0,'',Permission::orderBy('name','desc')->orderBy('updated_at','desc')->get()->toArray());
    }

    /**
     * 获取用户权限列表
     * @return [type] [description]
     */
    public function getUser(Request $request){
        $admin = Auth::guard('admin')->user();
        if($admin->isMaster){
            $this->ajaxJson(0,'',['isMaster'=>true]);
        }
        
        $permissions =array_pluck(DB::table('admin_permission')->select('name')->where('name','like','%'.$request->input('name').'%')->whereIn(
            'id',
            DB::table('admin_permission_role')->select('permission_id')->whereIn(
                'role_id',
                DB::table('admin_role_user')->select('role_id')->where(['admin_id'=>$admin->id])
            )
        )->get()->toArray(),'name');

        
        $this->ajaxJson(0,'',$permissions);
    }

    /**
     * 获取单条数据
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    function getOne($id){
    	$permission = $this->_get_one($id);
    	$this->ajaxJson(0,'',$permission);
    }

    /**
     * 验证权限路径唯一
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    function checkName(Request $request){
        $name = $request->input('name');
        $id   = $request->input('id');
        $this->checkUniqueName($id,$name);
        $this->ajaxJson(0,'');
    }


    /**
     * 更新单条数据
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    function postOne(Request $request){
    	$id = $request->input('id');
    	if($id){
    		$permission = $this->_get_one($id);
    	}else{
    		$permission = new Permission();
    	}
        $permission->name        = $request->input('name');
        $permission->displayName = $request->input('displayName');
        $permission->description = $request->input('description');
    	$permission->save();
    	$this->ajaxJson(0,'更新成功');
    }

    /**
     * 删除单条数据
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    function deleteOne($id){
    	$permission = $this->_get_one($id);
    	$permission->delete();
    	$this->ajaxJson(0,'删除成功');
    }

    /**
     * 验证权限名唯一
     * @param  [type] $id   [description]
     * @param  [type] $name [description]
     * @return [type]       [description]
     */
    private function checkUniqueName($id,$name){
        $count = Permission::where('id','!=',$id)->where('name',$name)->count();
        if($count>0){
            $this->ajaxJson(1,'存在相同的路径');
        }
    }

    /**
     * 公共获取单条数据
     * @return [type] [description]
     */
    private function _get_one($id){
    	$permission = Permission::find($id);
    	if(!$permission){
    		$this->ajaxJson(1,'数据不存在,请刷新页面');
    	}
    	return $permission;
    }
}
